﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dotnet.FormExtensions
{
    public static partial class Ext
    {
        /// <summary>
        /// 转换DataGridView列为Checkbox并添加到面板Panel
        /// </summary>
        /// <param name="panel">面板</param>
        /// <param name="dgvMain">当前数据源</param>
        /// <param name="action">string:当前Checkbox名称，bool：Checked状态值 </param>
        public static void AddCheckboxFromDataGridColumns(this DataGridView dgv,Panel panel, Action<string, bool> action = null)
        {
            if (dgv == null)
                return;
            panel.Controls.Clear();
            try
            {
                var cols = dgv.Columns.OfType<DataGridViewColumn>().OrderBy(col => col.Index).ToList();
                for (int i = 0; i <= cols.Count; i++)
                {
                    CheckBox chb = new CheckBox();
                    chb.AutoSize = true;
                    chb.Size = new System.Drawing.Size(101, 19);
                    chb.Margin = new System.Windows.Forms.Padding(0);
                    chb.Location = new System.Drawing.Point(15, 10 + 30 * i);
                    chb.TabIndex = i + 1;
                    chb.Name = "chb" + i.ToString();
                    chb.Checked = true;
                    chb.UseVisualStyleBackColor = true;
                    if (i == 0)
                    {
                        chb.Text = "全选";
                    }
                    else
                    {
                        chb.Text = cols[i - 1].HeaderText;
                    }
                    panel.Controls.Add(chb);
                    chb.CheckedChanged += new EventHandler((sender, e) =>
                    {
                        CheckBox chb1 = sender as CheckBox;
                        if (chb1 == null)
                            return;
                        chb1.BackColor = Color.Transparent;
                        if (chb1.Text == "全选")
                        {
                            panel.Controls.Cast<Control>().ToList().ForEach(c =>
                            {
                                CheckBox chb2 = c as CheckBox;
                                chb2.Checked = chb1.Checked;
                            });
                        }
                        else
                        {
                            DataGridViewColumn column = dgv.Columns.OfType<DataGridViewColumn>().FirstOrDefault(t => t.HeaderText == chb1.Text);
                            if (column == null)
                                return;
                            column.Visible = chb1.Checked;
                            if (action != null)
                            {
                                action(column.Name, chb1.Checked);
                            }
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 将DataGridView指定行带到顶端并指定颜色。
        /// </summary>
        /// <param name="dgv"></param>
        public static void RowToTop(this DataGridView dgv,int row,Color c)
        {
            dgv.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.DefaultCellStyle.BackColor = Color.White);
            dgv.Rows[row].Selected = true;
            dgv.ClearSelection();
            dgv.Rows[row].DefaultCellStyle.BackColor = c;
            dgv.FirstDisplayedScrollingRowIndex = row;
        }

        public static void RowToTop(this DataGridView dgv, int row)
        {
            RowToTop(dgv, row, Color.Aqua);
        }
        /// <summary>
        /// 将指定行带到顶端并加颜色
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="row"></param>
        public static void RowToTop(this DataGridView dgv,DataGridViewRow row)
        {
            if (row == null)
            {
                return;
            }
            RowToTop(dgv,dgv.Rows.IndexOf(row));
        }
        /// <summary>
        /// 将指定行带到顶端并加颜色
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="row"></param>
        public static void RowToTop(this DataGridView dgv, DataGridViewRow row,Color c)
        {
            if (row == null)
            {
                return;
            }
            RowToTop(dgv, dgv.Rows.IndexOf(row),c);
        }
        public static void ResetColor(this DataGridView dgv)
        {
            dgv.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.DefaultCellStyle.BackColor = Color.White);
            //dgv.Rows[row].Selected = true;
            //dgv.ClearSelection();
            //dgv.Rows[row].DefaultCellStyle.BackColor = c;
            //dgv.FirstDisplayedScrollingRowIndex = row;
        }


        /// <summary>
        /// 选中指定列并标记颜色
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="colName"></param>
        /// <param name="c"></param>
        public static void ColToTop(this DataGridView dgv,string colName,Color c)
        {
            if (dgv.Columns.Count == 0)
                return;
            if (string.IsNullOrEmpty(colName) == true)
                return;
            dgv.Columns.OfType<DataGridViewColumn>().ToList().ForEach(t => t.DefaultCellStyle.BackColor = Color.White);
            var col = dgv.Columns[colName];
            if (col == null)
                return;
            dgv.Columns[colName].DefaultCellStyle.BackColor = c;
            if (dgv.CurrentCell == null)
                return;
            if (dgv.CurrentCell.OwningColumn.Visible == false)
                return;
            if (dgv.Rows.Count > 0)
                dgv.CurrentCell = dgv.Rows[0].Cells[colName];  //带到当前列
            dgv.ClearSelection();
        }
        public static void ColToTop(this DataGridView dgv, string colName)
        {
            ColToTop(dgv, colName, Color.Aqua);
        }
    }
}
